package com.fulihui.wgj.admin.integration.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.result.web.Result;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.wgj.admin.api.region.request.RegionQueryProvinceRequest;
import com.fulihui.wgj.admin.api.region.vo.WxRegionVO;
import com.fulihui.wgj.admin.integration.WxRegionServiceClient;
import com.fulihui.wgj.facade.dto.WxRegionDTO;
import com.fulihui.wgj.facade.request.region.WxRegionQueryProvinceRequest;
import com.fulihui.wgj.facade.service.region.WxRegionService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: Levon
 * @date: 2018-07-24 17:15
 */
@Service
public class WxRegionServiceClientImpl implements WxRegionServiceClient {

    @Reference(version = "1.0.0")
    private WxRegionService wxRegionService;

    @Override
    public Result<List<WxRegionVO>> queryCountry() {
        SimpleResult<List<WxRegionDTO>> simpleResult = wxRegionService.queryAllCountry();
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            List<WxRegionDTO> data = simpleResult.getData();
            List<WxRegionVO> regionVOS = BeanConv.copy(data, WxRegionVO.class);
            return JsonResultBuilder.succ(regionVOS);
        }
        return JsonResultBuilder.fail(simpleResult.getErrcode(), simpleResult.getErrmsg());
    }

    @Override
    public Result<List<WxRegionVO>> queryProvince(RegionQueryProvinceRequest request) {
        WxRegionQueryProvinceRequest queryProvinceRequest = new WxRegionQueryProvinceRequest();
        queryProvinceRequest.setCountryTab(request.getCountryTab());
        queryProvinceRequest.setCountryIds(request.getCountryIds());
        SimpleResult<List<WxRegionDTO>> simpleResult = wxRegionService.queryProvince(queryProvinceRequest);
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            List<WxRegionDTO> data = simpleResult.getData();
            List<WxRegionVO> regionVOS = BeanConv.copy(data, WxRegionVO.class);
            return JsonResultBuilder.succ(regionVOS);
        }
        return JsonResultBuilder.fail(simpleResult.getErrcode(), simpleResult.getErrmsg());
    }
}
